/*
 	Software Author: Mike Moore
 	Vectrics Flashrecruit
 	Open source job board and applicant tracking system.  Built with enterprise Java.
 	GPL License applies to this source code.
 */
package org.vectrics.struts;

import java.security.Principal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.vectrics.SystemException;
import org.vectrics.common.util.StringUtils;

public class Request {
	private String contextPath = "";
	private Properties properties = new Properties();
	private Locale locale = Locale.US;	
	private Properties parameters = new Properties();
	protected Map<String, Object> attributes = new HashMap<String, Object>();
	protected Map<String, Object> sessionAttributes = new HashMap<String, Object>();
	private StringBuffer requestURL = null;
	private String requestURI = null;
	private String queryString = null;
	private String sessionId = null;
	private HttpSession session = null;
	private String remoteHost = null;
	private String remoteAddr = null;
	private Principal userPrincipal = null;
	private HttpServletRequest httpRequest = null;
	
	/**
	 * @return the session
	 */
	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}
	
	public Set<String> getAttributeNames() {
		return(this.attributes.keySet());
	}
	
	public Set<String> getSessionAttributeNames() {
		return(this.sessionAttributes.keySet());
	}
	
	public Set<String> getParameterNames() {
		Set<String> parameterNames = new HashSet<String>();
		Iterator iter =  this.parameters.keySet().iterator();
		while (iter.hasNext()) {
			String paramName = (String)iter.next();
			parameterNames.add(paramName);
		}
		return(parameterNames);
	}
	
	public void clearParameters() {
		this.parameters.clear();
		if (this.httpRequest != null) {
			this.httpRequest.getParameterMap().clear();
		}
	}
	
	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}
	
	public String getRequestURI() {
		return requestURI;
	}

	public void setRequestURI(String requestURI) {
		this.requestURI = requestURI;
	}

	public StringBuffer getRequestURL() {
		return(requestURL);
	}
	
	public void setRequestURL(StringBuffer url) {
		this.requestURL = url;
	}
	
	public void addParameter(String name, String value) {
		parameters.setProperty(name, value);
	}
	
	public String getParameter(String name) {
		return(parameters.getProperty(name));
	}
	
	public void setAttribute(String name, Object object) {
		attributes.put(name, object);
		if (this.httpRequest != null) {
			this.httpRequest.setAttribute(name, object);
		}
	}

	public Object getAttribute(String name) {
		return(attributes.get(name));
	}
	
	public void setSessionAttribute(String name, Object object) {
		sessionAttributes.put(name, object);
		if (this.session != null) {
			this.session.setAttribute(name, object);
		} 
	}
	
	public void addSessionAttribute(String name, Object object) {
		sessionAttributes.put(name, object);
		if (this.session != null) {
			this.session.setAttribute(name, object);
		} 
	}
	
	public void removeSessionAttribute(String name) {
		sessionAttributes.remove(name);
		if (this.session != null) {
			this.session.removeAttribute(name);
		} 
	}

	public Object getSessionAttribute(String name) {
		Object resultado = sessionAttributes.get(name);
		if (resultado==null){
			resultado = this.getSession().getAttribute(name);
		}
		return resultado;
	}

	public String getContextPath() {
		return contextPath;
	}

	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	
	public void setSessionAttributes(Map<String, Object> sessionAttributes) {
		this.sessionAttributes = sessionAttributes;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public String getRemoteAddr() {
		return remoteAddr;
	}

	public void setRemoteAddr(String remoteAddr) {
		this.remoteAddr = remoteAddr;
	}

	public String getRemoteHost() {
		return remoteHost;
	}

	public void setRemoteHost(String remoteHost) {
		this.remoteHost = remoteHost;
	}

	public Principal getUserPrincipal() {
		return userPrincipal;
	}

	public void setUserPrincipal(Principal userPrincipal) {
		this.userPrincipal = userPrincipal;
	}

	public HttpServletRequest getHttpRequest() {
		return httpRequest;
	}

	public void setHttpRequest(HttpServletRequest httpRequest) {
		this.httpRequest = httpRequest;
	}
	
	public Long getLongParameter(String name) {
		String valueStr = this.getParameter(name);
		if (StringUtils.isNullOrBlank(valueStr)) {
			throw new SystemException("Could not find parameter: " + name);
		} 
		Long value = new Long(valueStr);
		return value;
	}
}
